<template>
  <div class="shopcart">
    <!-- 底部左侧 -->
    <!-- :class只有在totalcount大于0的时候才会有这个样式 -->
    <div class="content-left">
      <div class="logo-wrapper" :class="{'highlig':totalCount>0}">
        <span class="iconfont icon-gouwuchekong" @click="toggleList"></span>
        <i class="total" v-show="totalCount">{{totalCount}}</i>
      </div>
      <div class="desc-wrapper" :class="{'highlig':totalCount>0}">
        <p class="tip">另需{{poiInfo.shipping_fee_tip}}</p>
        <i class="total" v-show="totalPrice">总价：{{totalPrice}}￥</i>
      </div>
    </div>
    <!-- 底部右侧 -->
    <div class="content-right" :class="{'light':totalCount>0}">{{payStr}}</div>
    <!-- 购物车列表功能 -->
    <div class="shopcart-list" v-show="listShow" :class="{'show':listShow}">
      <div class="list-top" v-if="poiInfo.discounts2">{{poiInfo.discounts2[0].info}}</div>
      <div class="list-header">
        <h3 class="title">1号口袋</h3>
        <div class="empty" @click="clearAll">
          <span>
            <i class="iconfont icon-column1"></i>
          </span>

          <span>清空购物车</span>
        </div>
      </div>
      <div class="list-content" ref="listContent">
        <ul>
          <li class="food-item" v-for="(food,index) in selectFoods" :key="index">
            <div class="desc-wrapper">
              <div class="desc-left">
                <p class="name">{{food.name}}name</p>
                <p class="unit" v-show="!food.description">{{food.unit}}</p>
                <p class="description" v-show="!food.unit">{{food.description}}</p>
              </div>
              <div class="desc-right"></div>
            </div>
            <div class="cartcontrol-wrapper">
              <car-control :food="food"></car-control>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import carControl from "./../  cartControl/carControl.vue";
import BScroll from "better-scroll";
export default {
  data() {
    return {
      fold: true
    };
  },
  props: {
    poiInfo: {
      type: Object
      //   default:{}
    },
    selectFoods: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    //   计算购物车的价钱 只要把totalCount和totalPrice打到标签中就可以实现
    totalCount() {
      let num = 0;
      this.selectFoods.forEach(food => {
        num += food.count;
      });
      return num;
    },
    totalPrice() {
      let total = 0;
      this.selectFoods.forEach(food => {
        total += food.min_price * food.count;
      });
      return total;
    },
    // 底部右侧切换文字的东西
    payStr() {
      if (this.totalCount > 0) {
        return "去结算";
      } else {
        return this.poiInfo.min_price_tip;
      }
    },
    listShow() {
      if (!this.totalCount) {
        this.fold;
        return false;
      }
      let show = !this.fold;
      if (show) {
        // 实现滚动
        this.$nextTick(() => {
          if (!this.shopScroll) {
            this.shopScroll = new BScroll(this.$refs.listContent, {
              click: true
            });
          } else {
            // 进行一个刷新
            this.shopScroll.refresh();
          }
        });
      }
      return show;
    }
  },
  methods: {
    clearAll() {
      this.selectFoods.forEach(food => {
        food.count = 0;
      });
    },
    toggleList() {
      // 判断购物车个数是否为空
      if (!this.totalCount) {
        return;
      }
      this.fold = !this.fold;
    }
  },
  components: {
    carControl: carControl
  }
};
</script>
<style lang="less" scope>
.shopcart {
  width: 100%;
  height: 51px;
  background: #514f4f;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  z-index: 99;
  .content-left {
    //   左边需要变化
    flex: 1;
    // background-color: rgb(216, 42, 71);
    .logo-wrapper {
      width: 50px;
      height: 50px;
      background: #666666;
      border-radius: 50%;
      position: relative;
      top: -14px;
      left: 10px;
      text-align: center;
      float: left;
      line-height: 50px;
      .iconfont {
        font-size: 28px;
        color: #c4c4c4;
      }
    }
    .desc-wrapper {
      .tip {
        float: left;
        margin-left: 25px;
        font-size: 12px;
        color: #bab9b9;
        line-height: 51px;
      }
    }
  }
  .content-right {
    //   右边不用发生变化
    flex: 0 0 110px;
    // background-color: yellow;
    font-size: 15px;
    color: #bab9b9;
    line-height: 51px;
    text-align: center;
    font-weight: bold;
  }
}
.total {
  color: rgb(241, 245, 18);
}
.highlig {
  width: 100%;
  height: 100%;
  background-color: rgb(216, 42, 71);
}
.light {
  background-color: rgb(196, 58, 201);
}
.shopcart-list {
  position: absolute;
  top: -200px;
  left: 0;
  bottom: 50px;
  background-color: yellow;
  opacity: 0.9;
  width: 100%;
  height: 300px;
  overflow: hidden;
  font-size: 12px;
  color: red;
  z-index: -1;
}
// .list-content {
//   .name {
//     // float: left;
//   }
// }
</style>
